package org.example.design.memento.test1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author huangshen
 * @version 0
 * @description Gamer
 * @since 2022/2/11 15:15
 */
public class Gamer {

    private int money;

    private List<String> fruits = new ArrayList<>();

    private Random ran = new Random();

    private static String[] fruitsName = {
            "苹果","葡萄","香蕉","橘子"
    };

    public Gamer (int money) {
        this.money = money;
    }

    public int getMoney() {
        return money;
    }

    public void bet() {
        int dice = ran.nextInt(6) + 1;
        switch (dice) {
            case 1: {
                money += 100;
                System.out.println("所持金钱增加了");
                break;
            }
            case 2: {
                money /= 2;
                System.out.println("所持金钱减半了");
                break;
            }
            case 6: {
                String f = getFruit();
                System.out.println("获得了水果(" + f + ").");
                fruits.add(f);
                break;
            }
            default: {
                System.out.println("什么都没有发生");
                break;
            }
        }
    }

    public Memento createMemento() {
        Memento memento = new Memento(money);
        for (String fruit : fruits) {
            if(fruit.startsWith("好吃的")){
                memento.addFruit(fruit);
            }
        }
        return memento;
    }

    public void restoreMemento(Memento memento) {
        this.money = memento.getMoney();
        this.fruits = memento.getFruits();
    }

    @Override
    public String toString() {
        return "[money = " + money + ", fruits = " + fruits + "]";
    }

    private String getFruit() {
        String prefix = "";
        if(ran.nextBoolean()) {
            prefix = "好吃的";
        }
        return prefix + fruitsName[ran.nextInt(fruitsName.length)];
    }
}
